﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;


//Tcp服务器实例
public class TCPServer
{

    private static IDictionary<string,TCPAsyncServer> servers = new Dictionary<string, TCPAsyncServer>();

    /// <summary>
    /// 消息处理的委托
    /// </summary>
    /// <param name="header"></param>
    /// <param name="body"></param>
    public delegate void MainNotifyHandler(byte[] bytes);

    public delegate void SendFinishHandler(SendState state);

    public delegate void ReadErrorHandler(Exception e, ReadState clientState);
    public delegate void SendErrorHandler(Exception e, SendState clientState);
        

    /// <summary>
    /// 创建一个Server
    /// </summary>
    /// <param name="name"></param>
    /// <param name="instanceHeader">实例化Header的方法</param>
    /// <returns></returns>
    public static TCPAsyncServer CreateServer(string name)
    {
        if (servers.ContainsKey(name))
        {
            throw new Exception("TCP Server :" + name + " is exists!");
        }
        TCPAsyncServer asyncServer = new TCPAsyncServer();
        asyncServer.Name = name;
        servers.Add(name,asyncServer);
        return asyncServer;
    }

    public static TCPAsyncServer GetServer(string name)
    {
        if (servers.ContainsKey(name))
        {
            return servers[name];
        }
        return null;
    }
        
}
